<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data extends MY_Controller {
	private $list_cat;
	public function __construct(){
		parent::__construct();
		$this->list_cat = $this->cm->get_all($this->tbl->cat,array('mid'=>$this->mid),'',0,'a');
		$this->load->model('model_model','model');
	}
	
	function index(){

		$this->_data['opt_model'] = $this->model->get_opt();
		if($this->mid){
			$r_model = $this->cm->get_by_id($this->tbl->model,$this->mid);
			$this->_data['attr'] = unserialize($r_model->attr);

			$model_name = $r_model->mark;
			$tbl_name = CUSTOM_TBL_PREFIX.$model_name;
			
			$this->load->library('pagination');
		    $limit_num = !empty($this->p['per_page']) ? intval($this->p['per_page']) : 0;
			$conf_page = config_item('pagefollow');

			$list = $this->cm->get_page($tbl_name,'*',array(),array(),$limit_num,$conf_page['per_page'],'','a');
			$this->_data['list'] = $list;

		    //分页导航
		    $this->pagination->initialize($conf_page); 
	    	$this->pagination->base_url = $this->page_url();
		    $this->pagination->total_rows = $this->cm->count;

		    $this->_data['pagenav'] = $this->pagination->create_links();

		}

		$this->_data['opt_cat'] = getCatOpt($this->list_cat);
		$this->load->view('data/index',$this->_data);

	}

	function add(){

		$this->_data['opt_model'] = $this->model->get_opt();
		if($this->mid){
			$r_model = $this->cm->get_by_id($this->tbl->model,$this->mid);
			$this->_data['attr'] = unserialize($r_model->attr);
		}
		$this->_data['opt_cat'] = getCatOpt($this->list_cat);
		$this->load->view('data/add',$this->_data);

	}

	function add_do(){

		if(!$this->mid) die;

		$r_model = $this->cm->get_by_id($this->tbl->model,$this->mid,'a');
		if(empty($r_model['mark'])) die;

		$mark = $r_model['mark'];
		$opt_tbl = CUSTOM_TBL_PREFIX.$mark;

		if(file_exists(MODEL_DIR.'/'.$opt_tbl.'_model.php')){
			$this->load->model($opt_tbl.'_model','custom_model');
			$this->cm = $this->custom_model;
		}

		$this->cm->save($opt_tbl,$this->p);

		ejd('succ-reload');

	}

}

